package com.kitemap.widget.business
{
	import com.google.maps.LatLng;		import mx.collections.ArrayCollection;		import flash.events.Event;	import flash.events.EventDispatcher;	import flash.net.URLLoader;	import flash.net.URLRequest;	
	public class LocationManager extends EventDispatcher
    {
		private var _markers:ArrayCollection;
		
        public function addMarkerResult(success:Boolean):void {
        	if (success) {
	        	trace("LocationManager::addMarker: The marker was added to the database");
        	} else {
	        	trace("LocationManager::addMarker: For some reason, the marker was not added");
        	}
        }
        
        public function getAllResult(result:ArrayCollection):void
        {
        	trace("LocationManager::getAllResult");
        	_markers = result;
        	 dispatchEvent(new Event("markersChange"));
        }
        
        [Bindable (event="markersChange")]
        public function get markers():ArrayCollection
        {
        	trace("LocationManager::getAllResult");
        	return (_markers != null)?_markers:null;
        }

        public function handleFault(message:String):void {
            trace("Fault:" + message);
        }
		/* XML Services */
		public function getMarkersXml()
		{
			var xmlString:URLRequest = new URLRequest("markers.xml");
			var xmlLoader:URLLoader = new URLLoader(xmlString);
			xmlLoader.addEventListener("complete", readXml);
		}
		
		public function readXml(event:Event):void{
		    var markersXML:XML = new XML(event.target.data);
		    var markers_xml:XMLList = markersXML..marker;
		    var markersCount:int = markers_xml.length();
		    
		    //var markers:ArrayCollection = new ArrayCollection();
		    for (var i:Number = 0; i < markersCount; i++) {
		        var marker:XML = markers_xml[i];
		        var latlng:LatLng = new LatLng(marker.@lat, marker.@lng);
		        //createMarker(latlng, name, address, type);
		        markers.addItem({
		        	name:marker.@name,
		        	address:marker.@address,
		        	type:marker.@type
		        	
		        });
		    }
		    
		    _markers = markers; 
		}
		
 		
		
    }

}